import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class GUIParser {
   
   public GUIUnit parse(File input) throws IOException {
      final FileReader fileReader=new FileReader(input);
      final BufferedReader buffReader=new BufferedReader(fileReader);
      while(buffReader.ready()) {
         final String aLine=buffReader.readLine();
         if(!aLine.trim().isEmpty()) {
            parseLine(aLine);
         }
         currentLine++;
      }
      return root;
   } 
   final int INDENT_WIDTH=3;
   int currentLine=1;
   final List<GUIUnit> levels=new ArrayList<GUIUnit>();
   GUIUnit root;
   
   private void parseLine(String aLine) {
      final int indent=getIndent(aLine);
      final String id=aLine.trim(); //aLine.substring(indent*INDENT_WIDTH);
      if(id.isEmpty()) return;
      final GUIUnit n=GUIUnit.create(id);
      
      if(levels.isEmpty()) {
         root=n;
         levels.add(n); 
      } else {
         if(indent<0) {
            throw new RuntimeException();
         } else if(indent<=levels.size()) {
            if(indent==0) { 
               throw new RuntimeException("multiple roots at line "+currentLine);
            }
            levels.get(indent-1).addChild(n);
            if(indent<levels.size()) {
               levels.set(indent, n);    
               for(int i=levels.size()-1; i>indent; i--) {
                  levels.remove(i);
               }
            } else {
               levels.add(n);   
            }
         } else {
            throw new RuntimeException();
         }
      }
   }
   private int getIndent(String line) {
      int spaces=0;
      for(int i=0; i<line.length(); i++) {
         if(line.charAt(i)==' ') {
            spaces++;
         } else {
            break;
         }
      }
      if(spaces%INDENT_WIDTH!=0) {
         throw new RuntimeException("illegal indent at line "+currentLine);
      }
      return spaces/INDENT_WIDTH;
   }
   public static void main(String[] args) throws IOException {
      if(args.length!=1) throw new RuntimeException("Please specify input.");
      final GUIParser parser=new GUIParser();
      final String code=parser.parse(new File(args[0])).toCode();
      System.out.println(code);
      final FileWriter writer=new FileWriter(new File("src/GUI.java"));
      writer.append(code);
      writer.close();
   }
}
/*
GUIPackage
   DatabaseFrame
      FileMenu
         CreateTableMenuItem
         ExitMenuItem
      CenterPane
         TableListScrollPane
            TableList
         
   CreateTableDialog
      CenterPane
         FlowLayout
            Vertical
         TableNameField
         TableDescriptionField
         ColumnListScrollPane
            ColumnList
      BottomPane
         FlowLayout
            Horizontal
         OKButton
         CancelButton
         
   TableFrame
      TopPane
         FlowLayout
            Horizontal
         InsertButton
         ModifyButton
         DeleteButton
      CenterPane
         RecordTableScrollPane
            RecordTable
*/
